package com.fauks.ssm.mybatis.mapper.impl;

import com.fauks.ssm.mybatis.bean.MStudent;
import com.fauks.ssm.mybatis.mapper.IStudenDao;
import com.fauks.ssm.mybatis.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentDaoImpl implements IStudenDao {

    private SqlSession sqlSession;

    @Override
    public void insert(MStudent student) {
        try {
            sqlSession = MyBatisUtils.getSqlSession();
            //student命名空间
            sqlSession.insert("student.insert", student);
            //默认不自动提交事务，要手动提交
            sqlSession.commit();
            System.out.println("学生id=" + student.getId());
        } catch (Exception e) {
            e.printStackTrace();
            //回滚事务
            sqlSession.rollback();
        } finally {
            MyBatisUtils.closeSession();
        }
    }

    @Override
    public void insertAndGetId(MStudent student) {
        try {
            sqlSession = MyBatisUtils.getSqlSession();
            sqlSession.insert("insertAndGetId", student);
            sqlSession.commit();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    @Override
    public void deleteById(int id) {
        try {
            sqlSession = MyBatisUtils.getSqlSession();
            sqlSession.delete("deleteById", id);
            sqlSession.commit();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    @Override
    public void update(MStudent student) {
        try {
            sqlSession = MyBatisUtils.getSqlSession();
            sqlSession.update("updateStu", student);
            sqlSession.commit();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    @Override
    public List<MStudent> selectAllStudents() {
        List<MStudent> selectAllStu;
        try {
            sqlSession = MyBatisUtils.getSqlSession();
            selectAllStu = sqlSession.selectList("selectAllStu");
            sqlSession.commit();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        return selectAllStu;
    }

    //返回map集合，key为指定的字段查询出的值
    @Override
    public Map<String, Object> selectStudentsMap() {
        Map<String, Object> map;
        try {
            sqlSession = MyBatisUtils.getSqlSession();
            map = sqlSession.selectMap("selectAllStu", "name");
            sqlSession.commit();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        return map;

    }

    @Override
    public MStudent selectStudentById(int id) {
        MStudent student;
        try {
            sqlSession = MyBatisUtils.getSqlSession();
            student = sqlSession.selectOne("selectStudentById", id);
            sqlSession.commit();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        return student;
    }

    @Override
    public List<MStudent> selectStudentByName(String name) {
        List<MStudent> selectAllStu;
        try {
            sqlSession = MyBatisUtils.getSqlSession();
            selectAllStu = sqlSession.selectList("selectStudentByName", name);
            sqlSession.commit();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        return selectAllStu;
    }

    /**
     * 动态拼接查询条件
     *
     * @param name
     * @param age
     * @return
     */
    @Override
    public MStudent selectStuByCondition(String name, String age) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        Map<String, String> params = new HashMap<>();
        params.put("name", name);
        params.put("age", age);
        MStudent o = sqlSession.selectOne("selectByCondition", params);
        return o;
    }

    @Override
    public void updateByCondition(MStudent mStudent) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        try {
            Map<String, String> params = new HashMap<>();
            params.put("name", mStudent.getName());
            params.put("age", String.valueOf(mStudent.getAge()));
            params.put("id", String.valueOf(mStudent.getId()));
            sqlSession.update("updateByCondition", params);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            MyBatisUtils.closeSession();
        }
    }
}
